/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.equipment.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.emu.OnlineStatus;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.I18nUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.equipment.entity.EleEquipment;
import com.thinkgem.jeesite.modules.equipment.entity.EleEquipmentNocheck;
import com.thinkgem.jeesite.modules.equipment.entity.EleMonitorParam;
import com.thinkgem.jeesite.modules.equipment.service.EleEquipmentNocheckService;
import com.thinkgem.jeesite.modules.equipment.service.EleEquipmentService;
import com.thinkgem.jeesite.modules.equipment.service.EleMonitorParamService;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import com.thinkgem.jeesite.modules.sys.utils.SwitchUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 未审核接入设备管理Controller
 * @author ZC
 * @version 2017-06-20
 */
@Controller
@RequestMapping(value = "${adminPath}/equipment/eleEquipmentNocheck")
public class EleEquipmentNocheckController extends BaseController {

    @Autowired
    private EleEquipmentService eleEquipmentService;
    @Autowired
	private EleMonitorParamService eleMonitorParamService;
	@Autowired
	private EleEquipmentNocheckService eleEquipmentNocheckService;
	
	@ModelAttribute
	public EleEquipmentNocheck get(@RequestParam(required=false) String id) {
		EleEquipmentNocheck entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleEquipmentNocheckService.get(id);
		}
		if (entity == null){
			entity = new EleEquipmentNocheck();
		}
		return entity;
	}
	
	@RequiresPermissions("equipment:eleEquipmentNocheck:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleEquipmentNocheck eleEquipmentNocheck, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EleEquipmentNocheck> page = eleEquipmentNocheckService.findPage(new Page<EleEquipmentNocheck>(request, response), eleEquipmentNocheck); 
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		page.setLocale(locale);
		model.addAttribute("page", page);
		if(locale.getLanguage().equals("zh")) {
			model.addAttribute("language", locale.getLanguage()+"-cn");
		}else {
			model.addAttribute("language", locale.getLanguage());
		}
		return "modules/equipment/eleEquipmentNocheckList";
	}

	@RequiresPermissions("equipment:eleEquipmentNocheck:view")
	@RequestMapping(value = "form")
	public String form(EleEquipmentNocheck eleEquipmentNocheck, Model model) {
        User thisuser = UserUtils.getUser();
        eleEquipmentNocheck.setCompany(thisuser.getCompany());
        // 审核控制没有关闭 并且没有用户权限的时候
        if (!UserUtils.getSubject().isPermitted("equipment:eleEquipment:check")){
            eleEquipmentNocheck.setOffice(thisuser.getOffice());
        }
		model.addAttribute("eleEquipmentNocheck", eleEquipmentNocheck);
		return "modules/equipment/eleEquipmentNocheckForm";
	}

	@RequiresPermissions("equipment:eleEquipmentNocheck:view")
	@RequestMapping(value = "save")
	public String save(EleEquipmentNocheck eleEquipmentNocheck, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eleEquipmentNocheck)){
			return form(eleEquipmentNocheck, model);
		}
        eleEquipmentNocheckService.delete(eleEquipmentNocheck);
        System.out.println(JsonMapper.toJsonString(eleEquipmentNocheck));

        EleEquipment eleEquipment = (EleEquipment)JsonMapper.fromJsonString(JsonMapper.toJsonString(eleEquipmentNocheck), EleEquipment.class);
        eleEquipment.setCompany(eleEquipmentNocheck.getCompany());
        eleEquipment.setOffice(eleEquipmentNocheck.getOffice());
        eleEquipment.setType(eleEquipmentNocheck.getType());
        eleEquipment.setWidth(eleEquipmentNocheck.getWidth());
        eleEquipment.setHeight(eleEquipmentNocheck.getHeight());

        eleEquipment.setId(null);
        User thisuser = UserUtils.getUser();
        eleEquipment.setCreateBy(UserUtils.getUser());
        eleEquipment.setCheckDate(new Date());
     
        // 审核控制没有关闭 并且没有用户权限的时候
        if (!UserUtils.getSubject().isPermitted("equipment:eleEquipment:check") && !SwitchUtils.isClose("equipment:eleEquipment:check")){
            eleEquipment.setCheckBy(UserUtils.getCheckUser(""));
            eleEquipment.setDelFlag(EleEquipment.DEL_FLAG_AUDIT);
        }else{
            eleEquipment.setCheckBy(thisuser);
            eleEquipment.setDelFlag(EleEquipment.DEL_FLAG_NORMAL);
        }
        eleEquipmentService.save(eleEquipment);

		eleEquipmentService.updateOnlineStatus(eleEquipment.getId(), OnlineStatus.Online.getIndex());
		String locale = I18nUtils.getLocaleStr();
		String msg = "保存未审核接入设备成功";
		if(Global.US.equals(locale)){
			msg = "Save the success of unchecked access devices";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/equipment/eleEquipmentNocheck/?repage";
	}
	
	@RequiresPermissions("equipment:eleEquipmentNocheck:view")
	@RequestMapping(value = "delete")
	public String delete(EleEquipmentNocheck eleEquipmentNocheck, RedirectAttributes redirectAttributes) {
		eleEquipmentNocheckService.delete(eleEquipmentNocheck);
		String locale = I18nUtils.getLocaleStr();
		String msg = "删除未审核接入设备成功";
		if(Global.US.equals(locale)){
			msg = "Delete the success of unchecked access devices";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/equipment/eleEquipmentNocheck/?repage";
	}

}